//
//  SetPWDViewController.m
//  SafeFoodManagerDemo
//
//  Created by bob on 2017/10/16.
//  Copyright © 2017年 bob. All rights reserved.
//

#import "SetPWDViewController.h"
#import "TextFTableViewCell.h"
#import "OutLoginTableViewCell.h"
#import "ResetPWDModel.h"
@interface SetPWDViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)TB_TableView *mainTableView;

@property(nonatomic,strong)ResetPWDModel *model;

@end

@implementation SetPWDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.model=[ResetPWDModel new];
    
    self.title=@"修改密码";

    [self.view addSubview:self.mainTableView];
    
    // Do any additional setup after loading the view.
}


-(TB_TableView*)mainTableView
{
    if (!_mainTableView) {
        
        _mainTableView=[[TB_TableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
        _mainTableView.delegate=self;
        _mainTableView.dataSource=self;
        
        _mainTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        [_mainTableView registerNib:[UINib nibWithNibName:@"TextFTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        [_mainTableView registerNib:[UINib nibWithNibName:@"OutLoginTableViewCell" bundle:nil] forCellReuseIdentifier:@"SureCell"];
        
        _mainTableView.tableFooterView=[[UIView alloc]init];
        
    }
    
    return _mainTableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section==0) {
        
        return 3;
    }
    
    return 1;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        
          return  50*SCREEN_WIDTH/320.0;
        
    }
    
   return  44*SCREEN_WIDTH/320.0;
    
}


-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        
        TextFTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        
        cell.backgroundColor=TBColorTableViewBg;
        
        [cell.TB_textFile addTarget:self action:@selector(TextChange:) forControlEvents:UIControlEventEditingChanged];
        
        cell.TB_textFile.tag=indexPath.row+1000;
        
        cell.TB_textFile.secureTextEntry=YES;
        
        switch (indexPath.row) {
            case 0:
            {
                cell.placeHolder=@"原密码";
            }
                break;
                
            case 1:
            {
                cell.placeHolder=@"新密码";
            }
                break;
                
            default:
            {
                 cell.placeHolder=@"确认新密码";
            }
                break;
        }
        
        return cell;
        
    }else{
        
        OutLoginTableViewCell *Cell=[tableView dequeueReusableCellWithIdentifier:@"SureCell" forIndexPath:indexPath];
        
        Cell.title=@"确认提交";
        
        Cell.Titlecolor=[UIColor whiteColor];
        
        Cell.bgColor=TBColorProvideBtnbg;
        
        Cell.CellBgColor=TBColorTableViewBg;
        
        Cell.enable=NO;
        
        Cell.CLblock=^()
        {
            [self ChangePWD];
            
        };
        
        return Cell;
        
    }
    
  
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    if (section==0) {
        
        return 20;
    }
    
    return 0;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    if (section==0) {
        
        return 10;
    }
    
    return 0;
    
}

-(void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=TBColorTableViewBg;
    
}

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
    
    view.tintColor=TBColorTableViewBg;
}


-(void)TextChange:(UITextField*)text{
    
    NSInteger tags=text.tag-1000;
    
    switch (tags) {
        case 0:
        {
            self.model.oldPwd=text.text;
        }
            break;
            
        case 1:
        {
             self.model.NPwd=text.text;
        }
            break;
            
        default:
        {
             self.model.confirmPwd=text.text;
            
        }
            break;
    }
    
    //判断按钮是否可用
    
     OutLoginTableViewCell *cell=[self.mainTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    
    if (self.model.oldPwd.length!=0&&self.model.NPwd.length!=0&&self.model.confirmPwd.length!=0) {

        cell.enable=YES;
  
    }else{
        
        cell.enable=NO;
        
    }
    
}


-(void)ChangePWD
{
    
    NSDictionary *dict=self.model.mj_keyValues;
    
    NSLog(@"%@",dict);
    
    BaseReqApi *api=[[BaseReqApi alloc]initWithRequestUrl:@"/index.php/FoodCheckApi/modifyPwd.json" andrequestTime:5 andParams:dict andRequestMethod:YTKRequestMethodPOST andCache:NO andCacheTime:0 andPostToken:YES];
    
    [api StarRequest:^(TBResponseStatus responseStatus, NSString *message, id responseObject) {
       
        if (responseStatus==1) {
            
            [MBProgressHUD showSuccessMessage:@"修改成功"];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
            
        }else{
            
            [MBProgressHUD showErrorMessage:message];
            
        }
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
